﻿Imports Publics
Imports System.Windows.Forms
 
Public Class FrmBaoCaoNhapXuatTonVatTu
    Private arrTieuChi(2) As String
    Private intIndexTieuChi As Integer
    Private intIndexIn2Cap As Integer
    Private arrKieuIn2Cap As New ArrayList
    Private arrKieuIn3Cap As New ArrayList
    Private blnLaChonTieuChiDau As Boolean

    Private lstDataSource As New List(Of clsBaoCaoNhapXuatTonVatTu)
    Private lstDataSource_TMP As New List(Of clsBaoCaoNhapXuatTonVatTu)
    Private lstChiTiet As New List(Of clsBaoCaoNhapXuatTonVatTu)

    'Loc
    Dim sIDLoaiVT As String = String.Empty
    Private lstIDDonVi As New List(Of String)
    Private lstIDVatTu As New List(Of String)
    Private bolChonHet As Boolean

    Private _bQuyDoi As Boolean = False
    Public Property bQuyDoi() As Boolean
        Get
            Return _bQuyDoi
        End Get
        Set(ByVal value As Boolean)
            _bQuyDoi = value
        End Set
    End Property

    Private _MaKho As String
    Public Property strMaKho() As String
        Get
            Return _MaKho
        End Get
        Set(ByVal value As String)
            _MaKho = value
        End Set
    End Property

    Private Makho_TMP As String = String.Empty
    '
    Private _TuNgay As Date
    Public Property TuNgay() As Date
        Get
            Return _TuNgay
        End Get
        Set(ByVal value As Date)
            _TuNgay = value
        End Set
    End Property

    Private _DenNgay As Date
    Public Property DenNgay() As Date
        Get
            Return _DenNgay
        End Get
        Set(ByVal value As Date)
            _DenNgay = value
        End Set
    End Property

    Private _ThoiGianBC As String
    Public Property ThoiGianBC() As String
        Get
            Return _ThoiGianBC
        End Get
        Set(ByVal value As String)
            _ThoiGianBC = value
        End Set
    End Property

    Private _ChiInSL As Boolean
    Public Property ChiInSL() As Boolean
        Get
            Return _ChiInSL
        End Get
        Set(ByVal value As Boolean)
            _ChiInSL = value
        End Set
    End Property

    Private _lstSource_All As List(Of clsNhapXuatTonBaseVatTu)
    Public Property lstSource_All() As List(Of clsNhapXuatTonBaseVatTu)
        Get
            Return _lstSource_All
        End Get
        Set(ByVal value As List(Of clsNhapXuatTonBaseVatTu))
            _lstSource_All = value
        End Set
    End Property


    Private _LaNhapXuatTonKeHoach As Boolean
    Public Property LaNhapXuatTonKeHoach() As Boolean
        Get
            Return _LaNhapXuatTonKeHoach
        End Get
        Set(ByVal value As Boolean)
            _LaNhapXuatTonKeHoach = value
        End Set
    End Property

    Private Sub CreateListTieuChi()
        arrTieuChi(0) = "Theo vật tư"
        arrTieuChi(1) = "Theo kho"
        arrTieuChi(2) = "Theo loại vật tư"
    End Sub

    Private Sub CreateBarTieuChi(ByVal arrLst() As String)
        For i As Integer = 0 To arrTieuChi.Count - 1
            PopupMenu1.AddItem(New DevExpress.XtraBars.BarLargeButtonItem(BarManager1, arrTieuChi(i)))
        Next
    End Sub

    Private Sub CreateBarIn2Cap(ByVal arrTC() As String, ByVal intIndex As Integer)
        PopupMenu2.ClearLinks()
        arrKieuIn2Cap.Clear()
        For i As Integer = 0 To arrTieuChi.Count - 1
            If intIndex <> i Then
                arrKieuIn2Cap.Add(New With {.Name = "In " + arrTC(intIndex) + " + " + arrTC(i), .Chon = False})
                PopupMenu2.AddItem(New DevExpress.XtraBars.BarLargeButtonItem(BarManager1, arrKieuIn2Cap(i).Name))
            Else
                arrKieuIn2Cap.Add(New With {.Name = "In " + arrTC(intIndex), .Chon = False})
                PopupMenu2.AddItem(New DevExpress.XtraBars.BarLargeButtonItem(BarManager1, arrKieuIn2Cap(i).Name))
            End If
        Next
    End Sub

    Private Sub CreateBarIn3Cap(ByVal arrKieuIn As ArrayList)
        If intIndexTieuChi = 0 Then
            btnIn3Cap.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        Else
            btnIn3Cap.Visibility = DevExpress.XtraBars.BarItemVisibility.Always
        End If
        PopupMenu3.ClearLinks()
        arrKieuIn3Cap.Clear()
        For i As Integer = 0 To arrTieuChi.Count - 1
            If 0 <> i Then
                arrKieuIn3Cap.Add(New With {.Name = arrKieuIn(i).Name + " + " + arrTieuChi(0), .Chon = False})
                PopupMenu3.AddItem(New DevExpress.XtraBars.BarLargeButtonItem(BarManager1, arrKieuIn3Cap(i).Name))
            Else
                arrKieuIn3Cap.Add(New With {.Name = "In " + arrTieuChi(intIndexTieuChi), .Chon = False})
                PopupMenu3.AddItem(New DevExpress.XtraBars.BarLargeButtonItem(BarManager1, arrKieuIn3Cap(i).Name))
            End If
        Next
    End Sub

    Private Sub CreateBarIn(ByVal int As Integer)
        intIndexTieuChi = int
        If int = 1 Then
            grdcolMa.Caption = "Mã kho"
            grdcolTen.Caption = "Tên kho"
        ElseIf int = 0 Then
            grdcolMa.Caption = "Mã vật tư"
            grdcolTen.Caption = "Tên vật tư"
        Else
            grdcolMa.Caption = "Mã loại VT"
            grdcolTen.Caption = "Tên loại VT"
        End If
        banGrvNhapXuatTon.ViewCaption = String.Format("Báo cáo nhập xuất tồn kho vật tư từ ngày {0:dd/MM/yyy} đến ngày {1:dd/MM/yyyy}", TuNgay, DenNgay) '"Tổng hợp " + arrTieuChi(int) & ": " & ThoiGianBC
        CreateBarIn2Cap(arrTieuChi, intIndexTieuChi)
        CreateBarIn3Cap(arrKieuIn2Cap)
    End Sub

    Private Sub FormatColumn()
        Dim strFormatSL = "### ### ###" 'FormatNumberSL()
        Dim strFormatGiaTriSummaryItem_SX = "{0:### ### ###}"
        Dim strFormatGiaTri_SX = "### ### ###"


        grdcolSLNK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolSLNK.DisplayFormat.FormatString = strFormatSL
        grdcolSLNK.SummaryItem.DisplayFormat = "{0:" & strFormatSL & "}"
        '{0:### ### ### ### ### ### ##0}
        grdcolSLDK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolSLDK.DisplayFormat.FormatString = strFormatSL
        grdcolSLDK.SummaryItem.DisplayFormat = "{0:" & strFormatSL & "}"
        '
        grdcolSLXK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolSLXK.DisplayFormat.FormatString = strFormatSL
        grdcolSLXK.SummaryItem.DisplayFormat = "{0:" & strFormatSL & "}"

        '
        grdcolSLCK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolSLCK.DisplayFormat.FormatString = strFormatSL
        grdcolSLCK.SummaryItem.DisplayFormat = "{0:" & strFormatSL & "}"


        grdcolGVNK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolGVNK.DisplayFormat.FormatString = strFormatSL
        grdcolGVNK.SummaryItem.DisplayFormat = strFormatGiaTriSummaryItem_SX

        '
        grdcolGVDK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolGVDK.DisplayFormat.FormatString = strFormatGiaTri_SX
        grdcolGVDK.SummaryItem.DisplayFormat = strFormatGiaTriSummaryItem_SX
        '
        grdcolGVXK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolGVXK.DisplayFormat.FormatString = strFormatGiaTri_SX
        grdcolGVXK.SummaryItem.DisplayFormat = strFormatGiaTriSummaryItem_SX

        '
        grdcolGVCK.DisplayFormat.FormatType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        grdcolGVCK.DisplayFormat.FormatString = strFormatGiaTri_SX
        grdcolGVCK.SummaryItem.DisplayFormat = strFormatGiaTriSummaryItem_SX
    End Sub

    Private Sub FillDataToGrid(ByVal intIndexTC As Integer)

        Dim lstAllData = GetDataSource(intIndexTC, 0)
        lstDataSource_TMP = lstAllData
        lstDataSource.Clear()
        lstDataSource = (From ds In lstAllData Group ds By ds.IDTieuChi Into grp = Group _
                        Select New clsBaoCaoNhapXuatTonVatTu With { _
                        .IDTieuChi = IDTieuChi, .Ma = grp.FirstOrDefault.Ma, .Ten = grp.FirstOrDefault.Ten, _
                        .MaVT = grp.FirstOrDefault.MaVT, .TenVT = grp.FirstOrDefault.TenVT, _
                        .DVT = grp.FirstOrDefault.DVT, .DinhLuong = grp.FirstOrDefault.DinhLuong, _
                        .SLDK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.SLDK), _
                        .GVDK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.GVDK), _
                        .SLNK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.SLNK), _
                        .GVNK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.GVNK), _
                        .SLXK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.SLXK), _
                        .GVXK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.GVXK), _
                        .SLCK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.SLCK), _
                        .GVCK = grp.Sum(Function(c As clsBaoCaoNhapXuatTonVatTu) c.GVCK), _
                        .Chon = False}).ToList
        FillToGrid(lstDataSource)

    End Sub

    ''' <summary>
    ''' get source from source selected  
    ''' </summary>
    ''' <param name="intIndexTC"></param>
    ''' <param name="intIndex2Cap"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetDataSource(ByVal intIndexTC As Integer, ByVal intIndex2Cap As Integer) As List(Of clsBaoCaoNhapXuatTonVatTu)
        Dim lstAllData As New List(Of clsBaoCaoNhapXuatTonVatTu)
        For Each item In lstSource_All
            Dim objItem As New clsBaoCaoNhapXuatTonVatTu
            With objItem
                .ID_VatTu = item.IDVatTu
                .ID_Kho = item.IDKho
                .DVT = item.DVT
                .DinhLuong = item.DinhLuong
                .MaKho = item.MaKho
                .MaVT = item.MaVT
                .TenVT = item.TenVT
                .SLDK = item.SLDK
                .GVDK = item.GVDK
                .SLNK = item.SLNK
                .GVNK = item.GVNK
                .SLXK = item.SLXK
                .GVXK = item.GVXK
                .SLCK = item.SLDK + item.SLNK - item.SLXK
                .GVCK = item.GVDK + item.GVNK - item.GVXK

                .Chon = False
                Select Case intIndexTC
                    Case 0
                        .IDTieuChi = item.IDVatTu
                        .Ma = item.MaVT
                        .Ten = item.TenVT
                    Case 1
                        .IDTieuChi = item.IDKho
                        .Ma = item.MaKho
                        .Ten = item.TenKho
                    Case 2
                        .IDTieuChi = item.IDLoaiVT
                        .Ma = item.MaLoaiVT
                        .Ten = item.TenLoaiVT
                End Select
                If .Ma = "" AndAlso .Ten = "" Then
                    .IDTieuChi = ""
                    .Ma = "Trống mã"
                    .Ten = "Trống tên"
                End If
                'Cấp 2
                Select Case intIndex2Cap
                    Case 0
                        .Ma2 = item.MaVT
                        .Ten2 = item.TenVT
                    Case 1
                        .Ma2 = item.MaKho
                        .Ten2 = item.TenKho
                    Case 2
                        .Ma2 = item.MaLoaiVT
                        .Ten2 = item.TenLoaiVT
                End Select
                If .Ma2 = "" AndAlso .Ten2 = "" Then
                    .Ma2 = "Trống mã"
                    .Ten2 = "Trống tên"
                End If
            End With
            lstAllData.Add(objItem)
        Next

        'Lọc  
        Dim objVatuList = (From ds In dbTV.VT_DM_VatTus Where ds.InUsed Select ds).ToList
        Dim query = (From ds In lstAllData _
                     Where ds.ID_VatTu IsNot Nothing _
                     Join dsVT In dbTV.VT_DM_VatTus On dsVT.ID Equals ds.ID_VatTu _
                    Where ((lstIDVatTu Is Nothing Or lstIDVatTu.Count <= 0) OrElse lstIDVatTu.Contains(ds.ID_VatTu)) _
                        AndAlso ((lstIDDonVi Is Nothing Or lstIDDonVi.Count <= 0) OrElse lstIDDonVi.Contains(ds.ID_Kho)) _
                        AndAlso (String.IsNullOrEmpty(sIDLoaiVT)) _
                    Select ds).ToList
        If bQuyDoi Then
            'For Each item As clsBaoCaoNhapXuatTon In query
            '    Dim vt = objVatuList.Where(Function(c) c.ID = item.ID_VatTu).FirstOrDefault
            '    If vt IsNot Nothing Then
            '        'If Not String.IsNullOrEmpty(vt.DVT) AndAlso vt.DVT.ToLower <> "kg" Then
            '        '// Thiendv 20130504: Sua
            '        If Not String.IsNullOrEmpty(vt.DVT) AndAlso vt.DVT.ToUpper = "RAM" Then
            '            item.SLDK = item.SLDK.GetValueOrDefault * vt.SoKgRam.GetValueOrDefault
            '            item.SLNK = item.SLNK.GetValueOrDefault * vt.SoKgRam.GetValueOrDefault
            '            item.SLXK = item.SLXK.GetValueOrDefault * vt.SoKgRam.GetValueOrDefault
            '            item.SLCK = item.SLCK.GetValueOrDefault * vt.SoKgRam.GetValueOrDefault
            '        End If
            '    End If
            'Next
        End If
        '
        Return query
    End Function

    Private Sub SetSourceListReport(ByVal intIndexTC As Integer, ByVal intIndex2Cap As Integer)
        Dim listIDTieuChi As New List(Of Object)
        'Dim i As Integer
        banGrvNhapXuatTon.FocusedRowHandle += 1
        'If banGrvNhapXuatTon.RowCount > 0 Then
        '    For i = 0 To banGrvNhapXuatTon.RowCount
        '        If banGrvNhapXuatTon.GetRowCellValue(i, grdcolChon) = True Then
        '            listIDTieuChi.Add(banGrvNhapXuatTon.GetRowCellValue(i, grdcolIDTieuChi))
        '        End If
        '    Next
        'End If
        Dim lstKhoChonGrid As New List(Of clsDanhMucChon)
        Dim cls As clsDanhMucChon
        If lstDataSource.Count > 0 Then
            For Each item As clsBaoCaoNhapXuatTonVatTu In lstDataSource
                If item.Chon Then
                    listIDTieuChi.Add(item.IDTieuChi)
                    If intIndexTC = 1 Then
                        cls = New clsDanhMucChon
                        cls.Ma = item.Ma
                        cls.Ten = item.Ten
                        lstKhoChonGrid.Add(cls)
                    End If
                End If
            Next
        End If
        '
        'Makho_TMP
        Makho_TMP = "" ' GetMaKho(lstKhoChonGrid, strMaKho)
        '
        Dim lstAllData As List(Of clsBaoCaoNhapXuatTonVatTu) = GetDataSource(intIndexTC, intIndex2Cap)
        Dim lstSource As New List(Of clsBaoCaoNhapXuatTonVatTu)
        If listIDTieuChi.Count > 0 Then
            Dim ExitsNothing As Boolean = False
            Dim objExits = (From ds In listIDTieuChi Where ds Is Nothing Select ds).FirstOrDefault
            If objExits IsNot Nothing Then
                ExitsNothing = True
            End If

            For Each item In lstAllData
                If (ExitsNothing AndAlso item.IDTieuChi Is Nothing) OrElse listIDTieuChi.Contains(item.IDTieuChi) Then
                    lstSource.Add(item)
                End If
            Next
        Else
            lstSource = lstAllData
        End If

        lstChiTiet.Clear()
        For Each ctrl As clsBaoCaoNhapXuatTonVatTu In lstSource
            If String.IsNullOrEmpty(ctrl.IDTieuChi) Then
                ctrl.Ma = "Trống mã"
                ctrl.Ten = "Trống tên"
            End If
            If String.IsNullOrEmpty(ctrl.Ma2) Then
                ctrl.Ma2 = "Trống mã"
            End If
            If String.IsNullOrEmpty(ctrl.Ten2) Then
                ctrl.Ten2 = "Trống tên"
            End If

            lstChiTiet.Add(ctrl)
        Next
    End Sub
    ''' <summary>
    '''   Load source bindingSource
    ''' </summary>
    ''' <param name="lstSource"></param>
    ''' <remarks></remarks>
    Sub FillToGrid(ByVal lstSource As List(Of clsBaoCaoNhapXuatTonVatTu))
        BindingSource1.DataSource = lstDataSource
        grdNhapXuatTon.DataSource = BindingSource1.DataSource
        grdNhapXuatTon.RefreshDataSource()
    End Sub

    Private Sub In2Cap(ByVal intIndexTC As Integer, ByVal intIndex2Cap As Integer)
        'grdNhapXuatTon.Focus()
        'lstChiTiet.Clear()
        'banGrvNhapXuatTon.FocusedRowHandle += 1
        'If intIndex2Cap = intIndexTC Then
        '    InBaoCaoNhapXuatTon1Cap(lstDataSource, ThoiGianBC, arrTieuChi(intIndexTC).ToUpper, intIndexTC, 1, TuNgay, DenNgay, ChiInSL, strMaKho, bQuyDoi, LaNhapXuatTonKeHoach)

        'Else
        '    'Set source
        '    SetSourceListReport(intIndexTC, intIndex2Cap)
        '    'In 2 cấp
        '    InBaoCaoNhapXuatTon2Cap(lstChiTiet, ThoiGianBC, arrKieuIn2Cap(intIndexTC).Name.ToString.Remove(0, 2).ToUpper & " + " & arrTieuChi(intIndexIn2Cap).ToUpper, intIndexTC, 1, TuNgay, DenNgay, ChiInSL, Makho_TMP, bQuyDoi, LaNhapXuatTonKeHoach)

        'End If
    End Sub
    Private Sub In3Cap(ByVal intIndexTC As Integer, ByVal intIndex3Cap As Integer)
        'lstChiTiet.Clear()
        'banGrvNhapXuatTon.FocusedRowHandle += 1
        'If intIndexTC = intIndex3Cap Then
        '    '
        '    SetSourceListReport(intIndexTC, 0)
        '    '
        '    InBaoCaoNhapXuatTon2Cap(lstChiTiet, ThoiGianBC, arrKieuIn2Cap(intIndexTC).Name.ToString.Remove(0, 2).ToUpper & " + " & arrTieuChi(intIndexIn2Cap).ToUpper, intIndexTC, 1, TuNgay, DenNgay, ChiInSL, Makho_TMP, bQuyDoi, LaNhapXuatTonKeHoach)

        'ElseIf intIndex3Cap = 0 Then
        '    InBaoCaoNhapXuatTon1Cap(lstDataSource, ThoiGianBC, arrTieuChi(intIndexTC).ToUpper, intIndexTC, 1, TuNgay, DenNgay, ChiInSL, strMaKho, bQuyDoi, LaNhapXuatTonKeHoach)

        'Else
        '    '
        '    SetSourceListReport(intIndexTC, intIndex3Cap)
        '    '
        '    InBaoCaoNhapXuatTon3Cap(lstChiTiet, ThoiGianBC, arrKieuIn3Cap(intIndex3Cap).Name.ToString.Remove(0, 2).ToUpper, intIndexTC, 1, TuNgay, DenNgay, ChiInSL, Makho_TMP, bQuyDoi, LaNhapXuatTonKeHoach)

        'End If
    End Sub

    Private Sub FrmBaoCaoNhapXuatTon1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Escape Then
            Me.Close()
        End If
        If e.Modifiers = Windows.Forms.Keys.Control AndAlso e.KeyCode = Windows.Forms.Keys.F Then
            btnLoc_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Windows.Forms.Keys.A And e.Modifiers = Windows.Forms.Keys.Control Then
            If Not bolChonHet Then
                For i As Integer = 0 To banGrvNhapXuatTon.DataRowCount
                    banGrvNhapXuatTon.SetRowCellValue(i, grdcolChon, True)
                Next
                bolChonHet = True
            Else
                For i As Integer = 0 To banGrvNhapXuatTon.DataRowCount
                    banGrvNhapXuatTon.SetRowCellValue(i, grdcolChon, False)
                Next
                bolChonHet = False
            End If

            grdNhapXuatTon.RefreshDataSource()
            grdNhapXuatTon.Refresh()
        End If

        'If e.KeyCode = Windows.Forms.Keys.X And e.Modifiers = Windows.Forms.Keys.Control Then
        '    For i As Integer = 0 To banGrvNhapXuatTon.DataRowCount
        '        banGrvNhapXuatTon.SetRowCellValue(i, grdcolChon, False)
        '    Next
        '    grdNhapXuatTon.RefreshDataSource()
        '    grdNhapXuatTon.Refresh()
        'End If

    End Sub

    ''' <summary>
    ''' Hiển thị cột theo ChiInSL ( 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ViewColumn()
        If ChiInSL Then
            grdcolGVDK.Visible = False
            grdcolGVNK.Visible = False
            grdcolGVXK.Visible = False
            grdcolGVCK.Visible = False
        Else
            grdcolGVDK.Visible = True
            grdcolGVNK.Visible = True
            grdcolGVXK.Visible = True
            grdcolGVCK.Visible = True
        End If
    End Sub
    Private Sub FrmBaoCaoNhapXuatTon1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            'btnInNXBGDMN.Visibility = DevExpress.XtraBars.BarItemVisibility.Never

            '
            FormatColumn()
            'CreateColor()
            ViewColumn()
            Dim StyleFormatCondition1 As DevExpress.XtraGrid.StyleFormatCondition = New DevExpress.XtraGrid.StyleFormatCondition
            'StyleFormatCondition1.Appearance.BackColor = vKRowChon
            StyleFormatCondition1.Appearance.Options.UseBackColor = True
            StyleFormatCondition1.ApplyToRow = True
            StyleFormatCondition1.Column = grdcolChon
            StyleFormatCondition1.Condition = DevExpress.XtraGrid.FormatConditionEnum.Equal
            StyleFormatCondition1.Value1 = True
            Me.banGrvNhapXuatTon.FormatConditions.Add(StyleFormatCondition1)

            btnIn3Cap.Caption = BtnPrintLevel3Caption
            btnIn2Cap.Caption = BtnPrintLevel2Caption
            btnTieuChi.Caption = BtnTieuChiCaption
            btnChiTiet.Caption = BtnDetailCaption
            btnDong.Caption = BtnCloseCaption
            Me.Text = "Báo cáo nhập xuất tồn vật tư"

            CreateListTieuChi()
            CreateBarTieuChi(arrTieuChi)
            FillDataToGrid(0)
            CreateBarIn(0)

        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChiTiet.ItemClick
        'Dim listIDTieuChi As New List(Of Object)

        'banGrvNhapXuatTon.FocusedRowHandle += 1

        'If lstDataSource.Count > 0 Then
        '    For Each item As clsBaoCaoNhapXuatTon In lstDataSource
        '        If item.Chon Then
        '            listIDTieuChi.Add(item.IDTieuChi)
        '        End If
        '    Next
        'End If
        'Dim lstSource As List(Of clsBaoCaoNhapXuatTon) = GetDataSource(intIndexTieuChi, 0)
        'Dim queryChiTiet As New List(Of clsBaoCaoNhapXuatTon)
        'If listIDTieuChi.Count > 0 Then
        '    Dim ExitsNothing As Boolean = False
        '    Dim objExits = (From ds In listIDTieuChi Where ds Is Nothing Select ds).FirstOrDefault
        '    If objExits IsNot Nothing Then
        '        ExitsNothing = True
        '    End If

        '    For Each item In lstSource
        '        If (ExitsNothing AndAlso item.IDTieuChi Is Nothing) OrElse listIDTieuChi.Contains(item.IDTieuChi) Then
        '            queryChiTiet.Add(item)
        '        End If
        '    Next
        'Else
        '    queryChiTiet = lstSource
        'End If

        'If queryChiTiet.Count > 0 Then
        '    Dim frmChiTiet As New FrmBaoCaoNhapXuatTonChiTiet
        '    frmChiTiet.IndexTieuChi = intIndexTieuChi
        '    frmChiTiet.banGrvNhapXuatTon.ViewCaption = banGrvNhapXuatTon.ViewCaption
        '    frmChiTiet.ThoiGianBC = ThoiGianBC
        '    frmChiTiet.ChiInSL = ChiInSL
        '    frmChiTiet.DataSourceT = (From ds In queryChiTiet _
        '                            Select New clsBaoCaoNhapXuatTon With { _
        '                            .IDTieuChi = ds.IDTieuChi, .Ma = ds.MaVT, .Ten = ds.TenVT, _
        '                            .SLDK = ds.SLDK, .GVDK = ds.GVDK, _
        '                            .SLNK = ds.SLNK, .GVNK = ds.GVNK, _
        '                            .SLXK = ds.SLXK, .GVXK = ds.GVXK, _
        '                            .SLCK = ds.SLCK, .GVCK = ds.GVCK}).ToList
        '    frmChiTiet.ShowDialog()
        'End If
    End Sub

    Private Sub btnTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTroGiup.ItemClick

    End Sub

    Private Sub btnDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnDong.ItemClick
        Me.Close()
    End Sub

    Private Sub BarManager1_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarManager1.ItemClick
        For i As Integer = 0 To arrTieuChi.Count - 1
            If arrTieuChi(i) = e.Item.Caption Then
                CreateBarIn(i)
                FillDataToGrid(i)
                Return
            End If
        Next
        'Duyet in 2 cap
        For i As Integer = 0 To arrKieuIn2Cap.Count - 1
            If arrKieuIn2Cap(i).Name = e.Item.Caption Then
                intIndexIn2Cap = i
                In2Cap(intIndexTieuChi, intIndexIn2Cap)
                Return
            End If
        Next
        'Duyet in 3 cap
        For i As Integer = 0 To arrKieuIn3Cap.Count - 1
            If arrKieuIn3Cap(i).Name = e.Item.Caption Then
                In3Cap(intIndexTieuChi, i)
                Return
            End If
        Next
    End Sub

    Private Sub banGrvNhapXuatTon_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Windows.Forms.Keys.Space Then
            If banGrvNhapXuatTon.GetRowCellValue(banGrvNhapXuatTon.FocusedRowHandle, grdcolChon) = False Then
                banGrvNhapXuatTon.SetRowCellValue(banGrvNhapXuatTon.FocusedRowHandle, grdcolChon, True)
            Else
                banGrvNhapXuatTon.SetRowCellValue(banGrvNhapXuatTon.FocusedRowHandle, grdcolChon, False)
            End If
            grdNhapXuatTon.RefreshDataSource()
            grdNhapXuatTon.Refresh()
        End If

        If e.Modifiers = Windows.Forms.Keys.Control AndAlso e.KeyCode = Windows.Forms.Keys.A Then
            For i = 0 To banGrvNhapXuatTon.RowCount - 1
                banGrvNhapXuatTon.SetRowCellValue(i, grdcolChon, True)
            Next
            grdNhapXuatTon.RefreshDataSource()
            grdNhapXuatTon.Refresh()
        ElseIf e.Modifiers = Windows.Forms.Keys.Control AndAlso e.KeyCode = Windows.Forms.Keys.X Then
            For i = 0 To banGrvNhapXuatTon.RowCount - 1
                banGrvNhapXuatTon.SetRowCellValue(i, grdcolChon, False)
            Next
            grdNhapXuatTon.RefreshDataSource()
            grdNhapXuatTon.Refresh()
        End If
    End Sub

    Private Sub btnXuat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuat.ItemClick
        'Dim saveDia As New 
        Export_Dialog(SaveFileDialog1, grdNhapXuatTon, "xls")
    End Sub
    Dim state As Boolean = False
    Private Sub btnLoc_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLoc.ItemClick
        'If state Then Return
        'state = True
        ''
        'sIDLoaiVT = String.Empty
        'lstIDDonVi.Clear()
        'lstIDVatTu.Clear()
        'bQuyDoi = False
        ''
        'Dim frm As New FrmLocBaoCaoNhapXuatTon
        'frm.lstDataSourceFilter = lstDataSource_TMP
        'frm.ShowDialog()
        'If frm.lstDataSourceFilter Is Nothing Then
        '    FillDataToGrid(intIndexTieuChi)
        'Else
        '    lstDataSource_TMP = frm.lstDataSourceFilter
        '    lstIDDonVi = frm.lstIDDonVi
        '    lstIDVatTu = frm.lstIDVatTu
        '    bQuyDoi = frm.QuyDoi
        '    '
        '    lstDataSource = (From ds In lstDataSource_TMP Group ds By ds.IDTieuChi Into grp = Group _
        '                Select New clsBaoCaoNhapXuatTon With { _
        '                .IDTieuChi = IDTieuChi, .Ma = grp.FirstOrDefault.Ma, .Ten = grp.FirstOrDefault.Ten, _
        '                .MaVT = grp.FirstOrDefault.MaVT, .TenVT = grp.FirstOrDefault.TenVT, _
        '                .MaKho = grp.FirstOrDefault.MaKho, _
        '                .SLDK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.SLDK), _
        '                .GVDK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.GVDK), _
        '                .SLNK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.SLNK), _
        '                .GVNK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.GVNK), _
        '                .SLXK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.SLXK), _
        '                .GVXK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.GVXK), _
        '                .SLCK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.SLCK), _
        '                .GVCK = grp.Sum(Function(c As clsBaoCaoNhapXuatTon) c.GVCK), _
        '                .Chon = False}).ToList
        '    FillToGrid(lstDataSource)
        '    'grdNhapXuatTon.DataSource = lstDataSource
        '    'grdNhapXuatTon.RefreshDataSource()
        '    'grdNhapXuatTon.Refresh()
        'End If
        'state = False
    End Sub

    Private Sub btnNXTVTGiay_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        'Dim rpt As New rptNXBGDMN_BaoCaoNhapXuatTonVatTuGiay
        'Dim strPath As String = pathReportSanXuat & "\\" & "rptNXBGDMN_BaoCaoNhapXuatTonVatTuGiay.repx"
        'Try
        '    rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        'Catch ex As Exception
        '    rpt = New rptNXBGDMN_BaoCaoNhapXuatTonVatTuGiay
        'End Try
        ''
        'rpt.ThoiGianBC = ThoiGianBC
        'rpt.TuNgay = TuNgay
        'rpt.DenNgay = DenNgay
        'rpt.lstIDKho = (From ds In lstSource_All Select ds.ID_Kho Distinct).ToList
        'rpt.lstIDVatTu = (From ds In lstSource_All Select ds.ID_VatTu Distinct).ToList
        ''
        'Dim frm As New FrmCustomReport(rpt, strPath, FrmCustomReport.NXBGDMN_BaoCaoNhapXuatTonVatTuGiay)
        'frm.WindowState = FormWindowState.Maximized
        'frm.ShowDialog()
    End Sub

    Private Sub btnNXTGiayDinhKy_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        'If TuNgay.Month = 1 AndAlso DenNgay.Month = 1 Then
        '    Dim rpt As New rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy_Thang1
        '    Dim strPath As String = pathReportSanXuat & "\\" & "rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy_Thang1.repx"
        '    Try
        '        rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        '    Catch ex As Exception
        '        rpt = New rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy_Thang1
        '    End Try
        '    '
        '    rpt.ThoiGianBC = ThoiGianBC
        '    rpt.TuNgay = TuNgay
        '    rpt.DenNgay = DenNgay
        '    rpt.lstIDKho = (From ds In lstSource_All Select ds.ID_Kho Distinct).ToList
        '    rpt.lstIDVatTu = (From ds In lstSource_All Select ds.ID_VatTu Distinct).ToList
        '    '
        '    Dim frm As New FrmCustomReport(rpt, strPath, FrmCustomReport.NXBGDMN_BaoCaoNhapXuatGiayDinhKy_Thang1)
        '    frm.WindowState = FormWindowState.Maximized
        '    frm.ShowDialog()
        'Else
        '    Dim rpt As New rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy
        '    Dim strPath As String = pathReportSanXuat & "\\" & "rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy.repx"
        '    Try
        '        rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        '    Catch ex As Exception
        '        rpt = New rptNXBGDMN_BaoCaoNhapXuatGiayDinhKy
        '    End Try
        '    '
        '    rpt.ThoiGianBC = ThoiGianBC
        '    rpt.TuNgay = TuNgay
        '    rpt.DenNgay = DenNgay
        '    rpt.lstIDKho = (From ds In lstSource_All Select ds.ID_Kho Distinct).ToList
        '    rpt.lstIDVatTu = (From ds In lstSource_All Select ds.ID_VatTu Distinct).ToList
        '    '
        '    Dim frm As New FrmCustomReport(rpt, strPath, FrmCustomReport.NXBGDMN_BaoCaoNhapXuatGiayDinhKy)
        '    frm.WindowState = FormWindowState.Maximized
        '    frm.ShowDialog()
        'End If
    End Sub

    Private Sub btnNXTTemChongGia_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        'Dim rpt As New rptNXBGDMN_BaoCaoNhapXuatTonTemChongGia
        'Dim strPath As String = pathReportSanXuat & "\\" & "rptNXBGDMN_BaoCaoNhapXuatTonTemChongGia.repx"
        'Try
        '    rpt = DevExpress.XtraReports.UI.XtraReport.FromFile(strPath, True)
        'Catch ex As Exception
        '    rpt = New rptNXBGDMN_BaoCaoNhapXuatTonTemChongGia
        'End Try
        ''
        'rpt.ThoiGianBC = ThoiGianBC
        'rpt.TuNgay = TuNgay
        'rpt.DenNgay = DenNgay
        'rpt.lstIDKho = (From ds In lstSource_All Select ds.ID_Kho Distinct).ToList
        'rpt.lstIDVatTu = (From ds In lstSource_All Select ds.ID_VatTu Distinct).ToList
        'rpt.lstSource_All = lstSource_All
        ''
        'Dim frm As New FrmCustomReport(rpt, strPath, FrmCustomReport.NXBGDMN_BaoCaoNhapXuatTonTemChongGia)
        'frm.WindowState = FormWindowState.Maximized
        'frm.ShowDialog()
    End Sub


End Class